<template>
  <div id="login">
    <van-nav-bar title="面经登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" color="#e97636" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <div class="bottom">
      <router-link to="/register">注册账号</router-link>
    </div>
  </div>
</template>

<script>
import { UserApi } from "@/api";

export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    onSubmit() {
      UserApi.login({
        username: this.username,
        password: this.password,
      })
        .then((res) => {
          console.log(res.data.data.token);
          this.$axios.saveAuth(res.data.data.token);
          this.$router.push("/layout/article/recommend");
        })
        .catch((e) => {
          console.log(e);
          this.$toast(e.response.data.message);
        });
    },
  },
};
</script>

<style scoped lang="less">
#login {
  width: 100%;
  height: 100%;
  .bottom {
    display: flex;
    justify-content: right;
    padding: 0 (20 / @vw);
  }
  a {
    font-size: (16 / @vw);
    color: #2b679a;
  }
}
</style>